﻿/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivityTrackingLog.Web.Helpers.ViewEngines;
using ActivityTrackingLog.Data;
using System.Web;
using ActivityTrackingLog.Web.Utils;
using System.Reflection;
using ActivityTrackingLog.Utils;

namespace ActivityTrackingLog.Web.Helpers
{
    public static class HtmlHelper
    {
        #region enums

        public enum EntityEnum
        {
            ActivityItem,
            UserAgentItem
        }

        public enum ViewTypeEnum
        {
            None,
            Page,
            Control,
            Data
        }

        public enum ReportTypeEnum
        {
            GroupedList,
            List,
            Chart
        }

        #endregion

        #region public methods

        public static string RenderList<T>(ViewTypeEnum viewType, bool embedThemeStyles, int pageIndex = 0, int pageSize = 20, ColumnData[] columns = null, string userId = null) where T : class
        {
            return Render<T>(viewType, ReportTypeEnum.List, embedThemeStyles, pageIndex, pageSize, columns, userId);
        }

        public static string RenderGroupedList<T>(ViewTypeEnum viewType, bool embedThemeStyles, int pageIndex = 0, int pageSize = 20, ColumnData[] columns = null, string userId = null) where T : class
        {
            return Render<T>(viewType, ReportTypeEnum.GroupedList, embedThemeStyles, pageIndex, pageSize, columns, userId);
        }

        public static string RenderChart<T>(ViewTypeEnum viewType, bool embedThemeStyles, string userId = null) where T : class
        {
            return Render<T>(viewType, ReportTypeEnum.Chart, embedThemeStyles, 0, 0, null, userId);
        }

        public static string Render<T>(
            ViewTypeEnum viewType, 
            ReportTypeEnum reportType, 
            bool embedThemeStyles, 
            int pageIndex, 
            int pageSize,
            ColumnData[] queryListColumns = null,
            string userId = null) where T : class
        {
            IViewEngine viewEngine = GetViewEngine<T>(viewType, reportType);
            // initialize view engine
            viewEngine.ReportBaseUrl = (null != HttpContext.Current)? HttpContext.Current.Request.Path: string.Empty;
            viewEngine.EmbedThemeStyles = embedThemeStyles;
            viewEngine.UserId = userId;
            viewEngine.PageIndex = pageIndex;
            viewEngine.PageSize = pageSize;

            var newCols = ProcessColumns<T>(queryListColumns);
            viewEngine.Columns = newCols;
            // render view content
            string content = viewEngine.RenderContent();
            return content;
        }

        #endregion

        #region helpers

        private static IViewEngine GetViewEngine<T>(ViewTypeEnum viewType, ReportTypeEnum reportType) where T : class
        {
            switch (reportType)
            {
                case ReportTypeEnum.List:
                    switch (viewType)
                    {
                        case ViewTypeEnum.Page:
                            return new ListPageRenderer<T>();
                        case ViewTypeEnum.Control:
                            return new ListControlRenderer<T>();
                        case ViewTypeEnum.Data:
                            return new ListDataRenderer<T>();
                    }
                    break;
                case ReportTypeEnum.GroupedList:
                    switch (viewType)
                    {
                        case ViewTypeEnum.Page:
                            return new GroupedListPageRenderer<T>();
                        case ViewTypeEnum.Control:
                            return new GroupedListControlRenderer<T>();
                        case ViewTypeEnum.Data:
                            return new GroupedListDataRenderer<T>();
                    }
                    break;
                case ReportTypeEnum.Chart:
                    switch (viewType)
                    {
                        case ViewTypeEnum.Page:
                            return new ChartPageRenderer<T>();
                        case ViewTypeEnum.Control:
                            return new ChartControlRenderer<T>();
                        case ViewTypeEnum.Data:
                            return new ChartDataRenderer<T>();
                    }
                    break;
            }

            return null;
        }

        private static ColumnData[] ProcessColumns<EntityType>(ColumnData[] columns)
        {
            Type objType = typeof(EntityType);
            var allProps = objType.GetProperties().Where(p => p.CanRead).ToList();
            if (null == columns)
                return allProps.Select(p => new ColumnData(p.Name, p)).ToArray();

            return columns
                .Where(c => {
                    var prop = allProps.Where(p => p.Name.CompareIgnoreCase(c.Name)).FirstOrDefault();
                    if (null != prop)
                    {
                        c.Name = prop.Name; // update col name
                        c.PropertyInfo = prop;
                    }
                    else
                        c.PropertyInfo = null;

                    return true;
                }).ToArray();
        }

        #endregion
    }
}
